package com.qianfeng.goods_7006.service.impl;

import com.qianfeng.entity.Goods;
import com.qianfeng.goods_7006.controller.GoodsController;
import com.qianfeng.goods_7006.dao.GoodsDao;
import com.qianfeng.goods_7006.dto.GoodsDto;
import com.qianfeng.goods_7006.service.GoodsService;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * author: 田晋宇
 * datetime:2021/11/3 16:06
 * email:853796359@qq.com
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private ElasticsearchRestTemplate template;


    private static final Logger logger = LoggerFactory.getLogger(GoodsController.class);



    @Override
    public List<Goods> getGoodsByType(Integer tid) {
        return goodsDao.getGoodsByType(tid);
    }

    @Override
    public void init() {

        


            IndexOperations ops = template.indexOps(GoodsDto.class);


            boolean exists = ops.exists();//判断索引是否存在
            System.out.println(exists);

            if(!exists){

                boolean b = ops.create();//创建索引

                System.out.println(b);
            }





            //和上一样
          /*  GoodsDto user1 = template.get("1", GoodsDto.class);
            System.out.println(user1);*/



        //查询所有
            SearchHits<GoodsDto> search = template.search(Query.findAll(), GoodsDto.class);
            if(search.getTotalHits()==0){//如果es查所有是0条记录 则查mysql
                List<Goods> goodsByType = goodsDao.getGoodsByType(null);
                ArrayList<GoodsDto> goodsDtos = new ArrayList<>();
                for (Goods goods : goodsByType) {
                    GoodsDto goodsDto = new GoodsDto();
                    goodsDto.setId(goods.getId());
                    goodsDto.setName(goods.getName());
                    goodsDto.setDetail(goods.getDetail());
                    goodsDto.setImgurls(goods.getImgurls()==null?null:goods.getImgurls().split(","));
                    goodsDto.setPacking(goods.getPacking()==null?null:goods.getPacking().split(","));
                    goodsDto.setPrice(goods.getPrice());
                    goodsDto.setTaste(goods.getTaste()==null?null:goods.getTaste().split(","));
                    goodsDto.setType(goods.getType()==null?null:goods.getType().split(","));
                    goodsDtos.add(goodsDto);
                }
                  
                    
                  template.save(goodsDtos);//批量添加


            }
            Iterator<SearchHit<GoodsDto>> iterator = search.iterator();
            while(iterator.hasNext()) {
                System.out.println(iterator.next());
            }



    }
}
